home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / jpegagasrc11.lha / jpegagasrc / jpegaga / jpegAGA.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-05  |  22.0 KB  |  639 lines

  1. /* jpegAGA written by Günther Röhrich                */
  2. /* this file is based on example.c, which is part of */
  3. /* the Independent JPEG Group's JPEG software        */
  4.  
  5. /* last change: 1-Oct-1994 */
  6.  
  7. #include "jinclude.h"
  8.  
  9.  
  10. /*
  11.  * <setjmp.h> is used for the optional error recovery mechanism shown in
  12.  * the second part of the example.
  13.  */
  14.  
  15. #include <setjmp.h>
  16. #include <signal.h> 
  17.  
  18. /* definitions for display.c */
  19.  
  20. #define HAM8 1
  21.  
  22. #ifdef __GNUC__
  23. #define MYSTRCMP strcasecmp
  24. #define MYSTRNCMP strncasecmp
  25. #else
  26. #define MYSTRCMP strcmp
  27. #define MYSTRNCMP strncmp
  28. #endif
  29.  
  30.  
  31. char  *ver = "\0$VER: jpegAGA 1.1 (5.10.94)";
  32. int VGAenable = 0;
  33. int SUPER72enable = 0;
  34. static int GrayEnable=0;
  35. static int BlockSmoothing=0;
  36. static FILE *ColorMapFile=NULL;
  37. char *MapFileName;
  38.  
  39. extern int InitDisplay(int cols, int rows, unsigned long Mode, int NumPlanes);
  40. extern void SetDisplayColor(int ColorNumber, unsigned char r, unsigned char g, unsigned char b);
  41. extern void CloseDisplay(void);
  42. extern void DisplayRow(char *array, int cols);
  43. extern int CheckButton(void);
  44. extern void FinalWait(void);
  45. JSAMPROW OutputBuffer=NULL;
  46.  
  47. extern void EncodeHAM8(char *rorig, char *gorig, char *borig, char *yham, int xsize);
  48. unsigned short Mult_Table[2*256];
  49.  
  50.  
  51. /* NOTE: this array is in brgbrg order */
  52. /* when a mapfile is available it will be overwritten */
  53.  
  54. char *ColorCache;
  55. unsigned char ColorTable[64*3] =
  56.  { 0, 0, 0,  4, 4, 4,  8, 8, 8, 12,12,12,   
  57.   16,16,16, 20,20,20, 24,24,24, 28,28,28,  /* 16 colors */
  58.   32,32,32, 36,36,36, 41,41,41, 46,46,46,
  59.   51,51,51, 55,55,55, 59,59,59, 63,63,63,
  60.  
  61.  
  62.                       17,17,39, 17,17,55, /* 13 colors */ 
  63.   17,29,17,           17,29,39, 17,29,55, 
  64.   17,39,17, 17,39,29, 17,39,39, 17,39,55,
  65.   17,55,17, 17,55,39, 17,55,39, 17,55,55,
  66.  
  67.  
  68.             29,17,29, 29,17,39, 29,17,55, /* 11 colors */
  69.                                 29,29,55,
  70.   29,39,17, 29,39,29,           29,39,55,
  71.   29,55,17, 29,55,29, 29,55,39, 29,55,55,
  72.  
  73.  
  74.   39,17,17, 39,17,29, 39,17,39, 39,17,55, /* 12 colors */
  75.   39,29,17, 39,29,29,           39,29,55,
  76.   39,39,17, 39,39,29,          
  77.   39,55,17, 39,55,29,  
  78.  
  79.   
  80.   55,17,17, 55,17,29, 55,17,39, 55,17,55, /* 13 colors */
  81.   55,29,27, 55,29,29, 55,29,39, 55,29,55,
  82.   55,39,17, 55,39,29, 55,39,39, 
  83.   55,55,17, 55,55,29
  84. };
  85.  
  86.  
  87.  
  88.  
  89.  
  90. /******************** JPEG DECOMPRESSION SAMPLE INTERFACE *******************/
  91.  
  92. /* This half of the example shows how to read data from the JPEG decompressor.
  93.  * It's a little more refined than the above in that we show how to do your
  94.  * own error recovery.  If you don't care about that, you don't need these
  95.  * next two routines.
  96.  */
  97.  
  98.  
  99. /*
  100.  * These routines replace the default trace/error routines included with the
  101.  * JPEG code.  The example trace_message routine shown here is actually the
  102.  * same as the standard one, but you could modify it if you don't want messages
  103.  * sent to stderr.  The example error_exit routine is set up to return
  104.  * control to read_JPEG_file() rather than calling exit().  You can use the
  105.  * same routines for both compression and decompression error recovery.
  106.  */
  107.  
  108. /* These static variables are needed by the error routines. */
  109. static jmp_buf setjmp_buffer;    /* for return to caller */
  110. static external_methods_ptr emethods; /* needed for access to message_parm */
  111.  
  112.  
  113. /* This routine is used for any and all trace, debug, or error printouts
  114.  * from the JPEG code.  The parameter is a printf format string; up to 8
  115.  * integer data values for the format string have been stored in the
  116.  * message_parm[] field of the external_methods struct.
  117.  */
  118.  
  119. METHODDEF void
  120. trace_message (const char *msgtext)
  121. {
  122.   fprintf(stderr, msgtext,
  123.       emethods->message_parm[0], emethods->message_parm[1],
  124.       emethods->message_parm[2], emethods->message_parm[3],
  125.       emethods->message_parm[4], emethods->message_parm[5],
  126.       emethods->message_parm[6], emethods->message_parm[7]);
  127.   fprintf(stderr, "\n");    /* there is no \n in the format string! */
  128. }
  129.  
  130. /*
  131.  * The error_exit() routine should not return to its caller.  The default
  132.  * routine calls exit(), but here we assume that we want to return to
  133.  * read_JPEG_file, which has set up a setjmp context for the purpose.
  134.  * You should make sure that the free_all method is called, either within
  135.  * error_exit or after the return to the outer-level routine.
  136.  */
  137.  
  138. void
  139. error_exit (const char *msgtext)
  140. {
  141.   trace_message(msgtext);    /* report the error message */
  142.   (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  143.   longjmp(setjmp_buffer, 1);    /* return control to outer routine */
  144. }
  145.  
  146.  
  147.  
  148. /*
  149.  * To accept the image data from decompression, you must define four routines
  150.  * output_init, put_color_map, put_pixel_rows, and output_term.
  151.  *
  152.  * You must understand the distinction between full color output mode
  153.  * (N independent color components) and colormapped output mode (a single
  154.  * output component representing an index into a color map).  You should use
  155.  * colormapped mode to write to a colormapped display screen or output file.
  156.  * Colormapped mode is also useful for reducing grayscale output to a small
  157.  * number of gray levels: when using the 1-pass quantizer on grayscale data,
  158.  * the colormap entries will be evenly spaced from 0 to MAX_JSAMPLE, so you
  159.  * can regard the indexes are directly representing gray levels at reduced
  160.  * precision.  In any other case, you should not depend on the colormap
  161.  * entries having any particular order.
  162.  * To get colormapped output, set cinfo->quantize_colors to TRUE and set
  163.  * cinfo->desired_number_of_colors to the maximum number of entries in the
  164.  * colormap.  This can be done either in your main routine or in
  165.  * d_ui_method_selection.  For grayscale quantization, also set
  166.  * cinfo->two_pass_quantize to FALSE to ensure the 1-pass quantizer is used
  167.  * (presently this is the default, but it may not be so in the future).
  168.  *
  169.  * The output file writing modules (jwrppm.c, jwrgif.c, jwrtarga.c, etc) may be
  170.  * useful examples of what these routines should actually do, although each of
  171.  * them is encrusted with a lot of specialized code for its own file format.
  172.  */
  173.  
  174.  
  175. METHODDEF void
  176. output_init (decompress_info_ptr cinfo)
  177. /* This routine should do any setup required */
  178. {
  179.   /* This routine can initialize for output based on the data passed in cinfo.
  180.    * Useful fields include:
  181.    *    image_width, image_height    Pretty obvious, I hope.
  182.    *    data_precision            bits per pixel value; typically 8.
  183.    *    out_color_space            output colorspace previously requested
  184.    *    color_out_comps            number of color components in same
  185.    *    final_out_comps            number of components actually output
  186.    * final_out_comps is 1 if quantize_colors is true, else it is equal to
  187.    * color_out_comps.
  188.    *
  189.    * If you have requested color quantization, the colormap is NOT yet set.
  190.    * You may wish to defer output initialization until put_color_map is called.
  191.    */
  192.  
  193.   int DisplaySuccess, i;
  194.   
  195.   if(cinfo->out_color_space == CS_GRAYSCALE)
  196.   {
  197.     printf("Width: %d, Height: %d\n", (int)cinfo->image_width, (int)cinfo->image_height);
  198.     DisplaySuccess = InitDisplay(cinfo->image_width, cinfo->image_height, 0, 8);
  199.     if(DisplaySuccess != 1)
  200.     {
  201.       CloseDisplay();
  202.       error_exit("Could not open display!");
  203.     }
  204.     for(i=0; i<256; i++) SetDisplayColor(i, (unsigned char)i, (unsigned char)i, (unsigned char)i);
  205.   }
  206.   else
  207.   {
  208.     ColorCache = calloc(262145, 1);
  209.     if(ColorCache == NULL) error_exit("Out of memory.");
  210.  
  211.     /* create the multiplication table */
  212.     for(i=-255; i<256; i++) Mult_Table[i+255] = (unsigned short)(i*i);
  213.  
  214.     printf("Using HAM8-Mode");
  215.  
  216.     strcat(MapFileName, ".map");
  217.     ColorMapFile = fopen(MapFileName, "r");
  218.     /* try to find the file in the directory pointed to by the environment */
  219.     /* variable MAPDIR to support read-only devices like CD-ROM */
  220.     if(!ColorMapFile)
  221.     {
  222.       char *MapDir;
  223.       MapDir = getenv("MAPDIR");
  224.       if(MapDir && (strlen(MapDir) != 0))
  225.       {
  226.         char *MapDirName;
  227.         int pos,i;
  228.         MapDirName = malloc(strlen(MapDir)+strlen(MapFileName)+5); /* worst case */
  229.         if(!MapDirName) error_exit("Out of memory.");
  230.         strcpy(MapDirName, MapDir);
  231.  
  232.         i = strlen(MapDirName);
  233.         if(MapDirName[i-1] != '/' && MapDirName[i-1] != ':')
  234.         {
  235.           strcat(MapDirName, "/");
  236.           i++;
  237.         }
  238.         i = strlen(MapFileName);
  239.         while(i > 0 && MapFileName[i-1] != '/' && MapFileName[i-1] != ':') i--;
  240.         strcat(MapDirName, &MapFileName[i]);
  241.         /* printf("%s\n", MapDirName); */
  242.         ColorMapFile = fopen(MapDirName, "r");
  243.       }
  244.     }
  245.  
  246.  
  247.     if(!ColorMapFile)
  248.     {
  249.       int i = strlen(MapFileName) - 4;
  250.       while(i > 0 && MapFileName[i-1] != '.') i--;
  251.       if(MapFileName[i-1] == '.')
  252.       {
  253.         strcpy(&MapFileName[i], "map");
  254.         ColorMapFile = fopen(MapFileName, "r");
  255.       }
  256.     }
  257.  
  258.  
  259.  
  260.     if(ColorMapFile)
  261.     {
  262.       unsigned short MagicNumber;
  263.       unsigned int Reserved;
  264.  
  265.       if(fread(&MagicNumber, 2, 1, ColorMapFile) != 1) error_exit("Read error in mapfile");
  266.       if(MagicNumber != 0x1203) error_exit("Wrong mapfile header!");    
  267.       if(fread(&Reserved,    4, 1, ColorMapFile) != 1) error_exit("Read error in mapfile");
  268.       if(fread(ColorTable,  64*3, 1, ColorMapFile) != 1) error_exit("Read error in mapfile");
  269.  
  270.       printf(" with mapfile");
  271.       fclose(ColorMapFile);
  272.       ColorMapFile = NULL;
  273.     }
  274.     else
  275.     {
  276.       printf(". Create a colormap file for better quality!");
  277.     }
  278.  
  279.     printf("\n");
  280.  
  281.     printf("Width: %d, Height: %d\n", (int)cinfo->image_width, (int)cinfo->image_height);
  282.  
  283.     DisplaySuccess = InitDisplay(cinfo->image_width, cinfo->image_height, HAM8, 8);
  284.     if(DisplaySuccess != 1)
  285.     {
  286.       CloseDisplay();
  287.       error_exit("Could not open display!");
  288.     }
  289.     for(i=0; i<64; i++) SetDisplayColor(i, ColorTable[i*3+1]<<2, 
  290.                                            ColorTable[i*3+2]<<2,
  291.                                            ColorTable[i*3]<<2);
  292.   }
  293.  
  294.   OutputBuffer = malloc(((cinfo->image_width+15)>>4)<<4);
  295.   if(!OutputBuffer) error_exit("Out of memory.");
  296.      
  297. }
  298.  
  299.  
  300. /*
  301.  * This routine is called if and only if you have set cinfo->quantize_colors
  302.  * to TRUE.  It is given the selected colormap and can complete any required
  303.  * initialization.  This call will occur after output_init and before any
  304.  * calls to put_pixel_rows.  Note that the colormap pointer is also placed
  305.  * in a cinfo field, whence it can be used by put_pixel_rows or output_term.
  306.  * num_colors will be less than or equal to desired_number_of_colors.
  307.  *
  308.  * The colormap data is supplied as a 2-D array of JSAMPLEs, indexed as
  309.  *        JSAMPLE colormap[component][indexvalue]
  310.  * where component runs from 0 to cinfo->color_out_comps-1, and indexvalue
  311.  * runs from 0 to num_colors-1.  Note that this is actually an array of
  312.  * pointers to arrays rather than a true 2D array, since C does not support
  313.  * variable-size multidimensional arrays.
  314.  * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
  315.  * to be as portable as the JPEG code proper, you should always access JSAMPLE
  316.  * values with the GETJSAMPLE() macro, which will do the right thing if the
  317.  * machine has only signed chars.
  318.  */
  319.  
  320. METHODDEF void
  321. put_color_map (decompress_info_ptr cinfo, int num_colors, JSAMPARRAY colormap)
  322. /* Write the color map */
  323. {
  324.   /* You need not provide this routine if you always set cinfo->quantize_colors
  325.    * FALSE; but a safer practice is to provide it and have it just print an
  326.    * error message, like this:
  327.    */
  328.   fprintf(stderr, "put_color_map called: there's a bug here somewhere!\n");
  329. }
  330.  
  331.  
  332. /*
  333.  * This function is called repeatedly, with a few more rows of pixels supplied
  334.  * on each call.  With the current JPEG code, some multiple of 8 rows will be
  335.  * passed on each call except the last, but it is extremely bad form to depend
  336.  * on this.  You CAN assume num_rows > 0.
  337.  * The data is supplied in top-to-bottom row order (the standard order within
  338.  * a JPEG file).  If you cannot readily use the data in that order, you'll
  339.  * need an intermediate array to hold the image.  See jwrrle.c for an example
  340.  * of outputting data in bottom-to-top order.
  341.  *
  342.  * The data is supplied as a 3-D array of JSAMPLEs, indexed as
  343.  *        JSAMPLE pixel_data[component][row][column]
  344.  * where component runs from 0 to cinfo->final_out_comps-1, row runs from 0 to
  345.  * num_rows-1, and column runs from 0 to cinfo->image_width-1 (column 0 is
  346.  * left edge of image).  Note that this is actually an array of pointers to
  347.  * pointers to arrays rather than a true 3D array, since C does not support
  348.  * variable-size multidimensional arrays.
  349.  * JSAMPLE is typically typedef'd as "unsigned char".  If you want your code
  350.  * to be as portable as the JPEG code proper, you should always access JSAMPLE
  351.  * values with the GETJSAMPLE() macro, which will do the right thing if the
  352.  * machine has only signed chars.
  353.  *
  354.  * If quantize_colors is true, then there is only one component, and its values
  355.  * are indexes into the previously supplied colormap.  Otherwise the values
  356.  * are actual data in your selected output colorspace.
  357.  */
  358.  
  359.  
  360. METHODDEF void
  361. put_pixel_rows (decompress_info_ptr cinfo, int num_rows, JSAMPIMAGE pixel_data)
  362. /* Write some rows of output data */
  363. {
  364.   /* This example shows how you might write full-color RGB data (3 components)
  365.    * to an output file in which the data is stored 3 bytes per pixel.
  366.    */
  367.   register JSAMPROW ptr0, ptr1, ptr2;
  368.   register int row;
  369.   
  370.   for (row = 0; row < num_rows; row++) 
  371.   {
  372.     ptr0 = pixel_data[0][row];
  373.     ptr1 = pixel_data[1][row];
  374.     ptr2 = pixel_data[2][row];
  375.  
  376.     if(CheckButton())
  377.     {
  378.       CloseDisplay();
  379.       error_exit("Display stopped.");
  380.     }
  381.  
  382.     EncodeHAM8(ptr0, ptr1, ptr2, OutputBuffer, cinfo->image_width);
  383.     
  384.     DisplayRow(OutputBuffer, cinfo->image_width);
  385.   }
  386. }
  387.  
  388. METHODDEF void
  389. put_gray_rows (decompress_info_ptr cinfo, int num_rows,
  390.            JSAMPIMAGE pixel_data)
  391. {
  392.   register JSAMPROW ptr0;
  393.   register long col;
  394.   long width = cinfo->image_width;
  395.   int row;
  396.  
  397.   if(CheckButton())
  398.   {
  399.     CloseDisplay();
  400.     error_exit("Display stopped.");
  401.   }
  402.  
  403.   for (row = 0; row < num_rows; row++)
  404.   {
  405.     memcpy(OutputBuffer, pixel_data[0][row], width);
  406.     DisplayRow(OutputBuffer, width);  
  407.   }
  408. }
  409.  
  410.  
  411. METHODDEF void
  412. output_term (decompress_info_ptr cinfo)
  413. /* Finish up at the end of the output */
  414. {
  415.   /* This termination routine may not need to do anything. */
  416.   /* Note that the JPEG code will only call it during successful exit; */
  417.   /* if you want it called during error exit, you gotta do that yourself. */
  418. }
  419.  
  420.  
  421. /*
  422.  * That's it for the routines that deal with writing the output image.
  423.  * Now we have overall control and parameter selection routines.
  424.  */
  425.  
  426.  
  427. /*
  428.  * This routine gets control after the JPEG file header has been read;
  429.  * at this point the image size and colorspace are known.
  430.  * The routine must determine what output routines are to be used, and make
  431.  * any decompression parameter changes that are desirable.  For example,
  432.  * if it is found that the JPEG file is grayscale, you might want to do
  433.  * things differently than if it is color.  You can also delay setting
  434.  * quantize_colors and associated options until this point. 
  435.  *
  436.  * j_d_defaults initializes out_color_space to CS_RGB.  If you want grayscale
  437.  * output you should set out_color_space to CS_GRAYSCALE.  Note that you can
  438.  * force grayscale output from a color JPEG file (though not vice versa).
  439.  */
  440.  
  441. METHODDEF void
  442. d_ui_method_selection (decompress_info_ptr cinfo)
  443. {
  444.   /* if grayscale input, force grayscale output; */
  445.   /* else leave the output colorspace as set by main routine. */
  446.   if (cinfo->jpeg_color_space == CS_GRAYSCALE || GrayEnable == 1)
  447.   {
  448.     cinfo->out_color_space = CS_GRAYSCALE; 
  449.     cinfo->methods->put_pixel_rows = put_gray_rows;
  450.   }
  451.   else
  452.   {
  453.     cinfo->methods->put_pixel_rows = put_pixel_rows; 
  454.   }
  455.     
  456.  
  457.   /* select output routines */
  458.   cinfo->methods->output_init = output_init;
  459.   cinfo->methods->put_color_map = put_color_map;
  460.   cinfo->methods->output_term = output_term;
  461. }
  462.  
  463.  
  464. /*
  465.  * OK, here is the main function that actually causes everything to happen.
  466.  * We assume here that the JPEG filename is supplied by the caller of this
  467.  * routine, and that all decompression parameters can be default values.
  468.  * The routine returns 1 if successful, 0 if not.
  469.  */
  470.  
  471. GLOBAL int
  472. read_JPEG_file (char * filename)
  473. {
  474.   /* These three structs contain JPEG parameters and working data.
  475.    * They must survive for the duration of parameter setup and one
  476.    * call to jpeg_decompress; typically, making them local data in the
  477.    * calling routine is the best strategy.
  478.    */
  479.   struct Decompress_info_struct cinfo;
  480.   struct Decompress_methods_struct dc_methods;
  481.   struct External_methods_struct e_methods;
  482.  
  483.   /* Select the input and output files.
  484.    * In this example we want to open the input file before doing anything else,
  485.    * so that the setjmp() error recovery below can assume the file is open.
  486.    * Note that cinfo.output_file is only used if your output handling routines
  487.    * use it; otherwise, you can just make it NULL.
  488.    * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
  489.    * requires it in order to read binary files.
  490.    */
  491.  
  492.   if ((cinfo.input_file = fopen(filename, "rb")) == NULL) {
  493.     fprintf(stderr, "can't open %s\n", filename);
  494.     return 0;
  495.   }
  496.  
  497.   cinfo.output_file = NULL;    /* if no actual output file involved */
  498.  
  499.   /* Initialize the system-dependent method pointers. */
  500.   cinfo.methods = &dc_methods;    /* links to method structs */
  501.   cinfo.emethods = &e_methods;
  502.   /* Here we supply our own error handler; compare to use of standard error
  503.    * handler in the previous write_JPEG_file example.
  504.    */
  505.   emethods = &e_methods;    /* save struct addr for possible access */
  506.   e_methods.error_exit = error_exit; /* supply error-exit routine */
  507.   e_methods.trace_message = trace_message; /* supply trace-message routine */
  508.   e_methods.trace_level = 0;    /* default = no tracing */
  509.   e_methods.num_warnings = 0;    /* no warnings emitted yet */
  510.   e_methods.first_warning_level = 0; /* display first corrupt-data warning */
  511.   e_methods.more_warning_level = 3; /* but suppress additional ones */
  512.  
  513.   /* prepare setjmp context for possible exit from error_exit */
  514.   if (setjmp(setjmp_buffer)) {
  515.     /* If we get here, the JPEG code has signaled an error.
  516.      * Memory allocation has already been cleaned up (see free_all call in
  517.      * error_exit), but we need to close the input file before returning.
  518.      * You might also need to close an output file, etc.
  519.      */
  520.     fclose(cinfo.input_file);
  521.     CloseDisplay();
  522.     return 10;
  523.   }
  524.  
  525.   /* Here we use the standard memory manager provided with the JPEG code.
  526.    * In some cases you might want to replace the memory manager, or at
  527.    * least the system-dependent part of it, with your own code.
  528.    */
  529.   jselmemmgr(&e_methods);    /* select std memory allocation routines */
  530.   /* If the decompressor requires full-image buffers (for two-pass color
  531.    * quantization or a noninterleaved JPEG file), it will create temporary
  532.    * files for anything that doesn't fit within the maximum-memory setting.
  533.    * You can change the default maximum-memory setting by changing
  534.    * e_methods.max_memory_to_use after jselmemmgr returns.
  535.    * On some systems you may also need to set up a signal handler to
  536.    * ensure that temporary files are deleted if the program is interrupted.
  537.    * (This is most important if you are on MS-DOS and use the jmemdos.c
  538.    * memory manager back end; it will try to grab extended memory for
  539.    * temp files, and that space will NOT be freed automatically.)
  540.    * See jcmain.c or jdmain.c for an example signal handler.
  541.    */
  542.  
  543.   /* Here, set up the pointer to your own routine for post-header-reading
  544.    * parameter selection.  You could also initialize the pointers to the
  545.    * output data handling routines here, if they are not dependent on the
  546.    * image type.
  547.    */
  548.   dc_methods.d_ui_method_selection = d_ui_method_selection;
  549.  
  550.   /* Set up default decompression parameters. */
  551.   j_d_defaults(&cinfo, TRUE);
  552.   /* TRUE indicates that an input buffer should be allocated.
  553.    * In unusual cases you may want to allocate the input buffer yourself;
  554.    * see jddeflts.c for commentary.
  555.    */
  556.  
  557.   /* At this point you can modify the default parameters set by j_d_defaults
  558.    * as needed; for example, you can request color quantization or force
  559.    * grayscale output.  See jdmain.c for examples of what you might change.
  560.    */
  561.  
  562.    /* if(GrayEnable) cinfo.out_color_space = CS_GRAYSCALE; */ /* force grayscale output */
  563.    if(BlockSmoothing) cinfo.do_block_smoothing = TRUE; 
  564.  
  565.   /* Set up to read a JFIF or baseline-JPEG file. */
  566.   /* This is the only JPEG file format currently supported. */
  567.   jselrjfif(&cinfo);
  568.  
  569.   /* Here we go! */
  570.   jpeg_decompress(&cinfo);
  571.  
  572.   /* That's it, son.  Nothin' else to do, except close files. */
  573.   /* Here we assume only the input file need be closed. */
  574.   fclose(cinfo.input_file);
  575.   FinalWait();
  576.   CloseDisplay();
  577.  
  578.   /* You might want to test e_methods.num_warnings to see if bad data was
  579.    * detected.  In this example, we just blindly forge ahead.
  580.    */
  581.   return 1;            /* indicate success */
  582.  
  583.   /* Note: if you want to decompress more than one image, we recommend you
  584.    * repeat this whole routine.  You MUST repeat the j_d_defaults()/alter
  585.    * parameters/jpeg_decompress() sequence, as some data structures allocated
  586.    * in j_d_defaults are freed upon exit from jpeg_decompress.
  587.    */
  588. }
  589.  
  590.  
  591. static void Usage(void)
  592. {
  593.  printf("Usage: jpegAGA file [switches]\n"
  594.         "switches: -GRAY    force grayscale output\n"
  595.         "          -BS      do block smoothing\n"
  596.         "          -VGA     use VGA screenmode\n"
  597.         "          -SUPER72 use SUPER72 mode for large pictures\n");
  598.  exit(10);
  599. }
  600.  
  601. int main(int argc, char *argv[])
  602. {
  603.  int i;
  604.  signal(SIGINT, SIG_IGN); /* disable CTRL-C handling */
  605.  printf("jpegAGA V1.1 by Günther Röhrich. This program is Public Domain.\n");
  606.  
  607.  /* remove the comments for beta versions */
  608.  /*  printf("Preliminary version. DO NOT SPREAD IT!\n"); */
  609.  
  610.  if(argc < 2) Usage();
  611.  
  612.  for(i=2; i<argc; i++)
  613.  {
  614.    #ifndef __GNUC__
  615.    strupr(argv[i]);
  616.    #endif
  617.    if(!MYSTRNCMP(argv[i], "-GRAY", 5)) GrayEnable=1;
  618.  
  619.    else if(!MYSTRNCMP(argv[i], "-BS", 3)) BlockSmoothing=1;    
  620.  
  621.    else if(!MYSTRNCMP(argv[i], "-VGA", 4)) VGAenable=1;
  622.  
  623.    else if(!MYSTRNCMP(argv[i], "-SUPER72", 8)) SUPER72enable=1;
  624.    
  625.    else Usage();
  626.  }    
  627.  
  628.  MapFileName = malloc(strlen(argv[1])+5);
  629.  if(MapFileName == NULL) 
  630.  {
  631.    printf("Out of memory.\n");
  632.    exit(20);
  633.  }
  634.  strcpy(MapFileName, argv[1]); /* create a copy of the file name */
  635.  read_JPEG_file(argv[1]);
  636.  return 0;
  637. }
  638.  
  639.